package com.ironone.mediaplayerdemo;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.GestureDetector;
import android.view.GestureDetector.SimpleOnGestureListener;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.Animation.AnimationListener;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.Toast;

public class Frag3 extends Fragment {
	FrameLayout fragContainer;
	ViewPager viewPager;
	Button btn1;
	View view;
	GestureDetector gestureDetector;
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

		// Inflate the layout for this fragment
		view = inflater.inflate(R.layout.frag_three, container, false);
		viewPager = (ViewPager) getActivity().findViewById(R.id.pager);

		fragContainer = (FrameLayout) getActivity().findViewById(R.id.fragContainer);
		btn1 = (Button) view.findViewById(R.id.close_button);

		gestureDetector = new GestureDetector(getActivity().getApplicationContext(), new GestureListener());
		
		//weekly pick close button
		btn1.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				/*AlphaAnimation fadeOutAnimation = new AlphaAnimation(1, 0); // start alpha, end alpha
				fadeOutAnimation.setDuration(500); // time for animation in milliseconds
				fadeOutAnimation.setFillAfter(true); // make the transformation persist
				fadeOutAnimation.setAnimationListener(new AnimationListener() {         
					@Override
					public void onAnimationEnd(Animation animation) {
						getActivity().getSupportFragmentManager().popBackStack();
						//fragContainer.setVisibility(View.GONE);
					}

					@Override
					public void onAnimationRepeat(Animation animation) { }

					@Override
					public void onAnimationStart(Animation animation) { }
				});
				fragContainer.setAnimation(fadeOutAnimation);*/

				ddd();

				//fragContainer.setVisibility(View.INVISIBLE);
				//fragContainer.setVisibility(View.GONE);
				/*Frag1 nextFrag= new Frag1();
				FragmentTransaction fragTransaction = getFragmentManager().beginTransaction();
				fragTransaction.setCustomAnimations(android.R.anim.fade_in, android.R.anim.fade_out);
				fragTransaction.replace(R.id.fragContainer, nextFrag);
				fragTransaction.addToBackStack(null);
				fragTransaction.commit();*/
			}
		});

		view.setOnTouchListener(new OnTouchListener() {
			
			@Override
			public boolean onTouch(View arg0, MotionEvent arg1) {
				
				return gestureDetector.onTouchEvent(arg1);
				
			}
		});

		return view;
	}

	private void ddd(){
		Animation animation = AnimationUtils.loadAnimation(this.getActivity(), R.anim.slide_middle_to_right);
		animation.setAnimationListener(new AnimationListener() {                  
			@Override
			public void onAnimationStart(Animation animation) {
				viewPager.setVisibility(View.VISIBLE);
				btn1.setClickable(false); //btn1.setEnabled(false);
			}

			@Override
			public void onAnimationRepeat(Animation animation) {

			}

			@Override
			public void onAnimationEnd(Animation animation) {
				getActivity().getSupportFragmentManager().popBackStack();
				fragContainer.setVisibility(View.GONE);
				btn1.setClickable(false); //Enabled(true);
				//viewPager.setEnabled(true);
			}
		});
		fragContainer.clearAnimation();
		fragContainer.startAnimation(animation);
	}






	private final class GestureListener extends SimpleOnGestureListener {

		private static final int SWIPE_THRESHOLD = 50;
		private static final int SWIPE_VELOCITY_THRESHOLD = 50;
		private static final int SWIPE_DISTANCE = 50;

		private boolean isFlingCall = false;

		@Override
		public boolean onDown(MotionEvent e) {
			// TODO Auto-generated method stub
			isFlingCall = false;
			return true;
		}

		@Override
		public boolean onSingleTapUp(MotionEvent e){    
			if(!isFlingCall) {
				onTouch();
				return false;
			}
			else {
				return true;
			}

		}
		@Override
		public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
				float velocityY) {
			try {
				float diffY = e2.getY() - e1.getY();
				float diffX = e2.getX() - e1.getX();
				if (Math.abs(diffX) > Math.abs(diffY)) {
					if (Math.abs(diffX) > SWIPE_THRESHOLD && Math.abs(velocityX) > SWIPE_VELOCITY_THRESHOLD) {
						if (diffX > SWIPE_DISTANCE) {
							isFlingCall = true;
							
							//
							
							onSwipeRight();
							return true;
						}
						else {
							onSwipeLeft();
							return true;
						}
					}
				}

			} catch (Exception exception) {
				exception.printStackTrace();
			}
			onTouch();
			return true;
		}
	}

	public void onSwipeRight() {
		//Toast.makeText(getActivity(), "to right", Toast.LENGTH_SHORT).show();
		ddd();
	}

	public void onSwipeLeft() {
		Toast.makeText(getActivity(), "to left", Toast.LENGTH_SHORT).show();
	}
	
	public void onTouch() {
	}



}
